package com.atguigui.leetcode1;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 2357.使数组中所有元素都等于零
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/2/24 9:30
 */
public class P2357MakeArrayZeroBySubtractingEqualAmounts {

    public static void main(String[] args) {
        Solution solution = new P2357MakeArrayZeroBySubtractingEqualAmounts().new Solution();
        // TO TEST
    }

    class Solution {
        public int minimumOperations(int[] nums) {

            int ans = 0;
            Arrays.sort(nums);
            int length = nums.length;

            for (int i = 0; i < length; i++) {
                if (nums[i] > 0) {
                    subtract(nums, nums[i], i);
                    ans++;
                }
            }
            return ans;
        }

        //直接模拟数组相减的过程
        public void subtract(int[] nums, int x, int startIndex) {
            int length = nums.length;
            for (int i = startIndex; i < length; i++) {
                nums[i] -= x;
            }
        }

        public int minimumOperations2(int[] nums) {
            Set<Integer> set = new HashSet<Integer>();
            for (int num : nums) {
                if (num > 0) {
                    set.add(num);
                }
            }
            return set.size();
        }
    }
}
